<template>
  <div class="form-gonggao">
    <el-card class="main-card">
      <div class="header">
        <h2 class="page-title">公告管理</h2>
        <el-button type="primary" class="add-button" @click="handleAdd">
          <el-icon><Plus /></el-icon>新增公告
        </el-button>
      </div>

      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{
          background: '#f5f7fa',
          color: '#606266',
        }"
        class="custom-table"
      >
        <el-table-column prop="title" label="公告标题" width="180" />
        <el-table-column prop="content" label="公告内容">
          <template #default="scope">
            <div class="content-cell" v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column prop="isVisible" label="是否显示" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.isVisible ? 'success' : 'danger'" class="status-tag">
              {{ scope.row.isVisible ? '显示' : '隐藏' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" width="180" />
        <el-table-column prop="updatedTime" label="更新时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增公告' : '编辑公告'"
      width="50%"
      class="custom-dialog"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="公告标题">
          <el-input v-model="form.title" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="公告内容">
          <wang v-model="form.content" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="form.isVisible" active-text="显示" inactive-text="隐藏" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import {
  getAnnouncementsUserList,
  deleteAnnouncement,
  createAnnouncement,
} from '@/api/zhaokao/systemweb';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Plus, Edit, Delete } from '@element-plus/icons-vue';

const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tableData = ref([]);
const dialogVisible = ref(false);
const dialogType = ref('add');
const form = ref({
  title: '',
  content: '',
  isVisible: true,
});

// 获取公告列表
const getList = async () => {
  try {
    const res = await getAnnouncementsUserList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    });
    tableData.value = res.data.records;
    total.value = res.data.total;
  } catch (error) {
    console.error('获取公告列表失败:', error);
  }
};

// 分页大小改变
const handleSizeChange = val => {
  pageSize.value = val;
  getList();
};

// 页码改变
const handleCurrentChange = val => {
  pageNum.value = val;
  getList();
};

// 新增公告
const handleAdd = () => {
  dialogType.value = 'add';
  form.value = {
    title: '',
    content: '',
    isVisible: true,
  };
  dialogVisible.value = true;
};

// 编辑公告
const handleEdit = row => {
  dialogType.value = 'edit';
  form.value = { ...row };
  dialogVisible.value = true;
};

// 删除公告
const handleDelete = row => {
  ElMessageBox.confirm('确定要删除该公告吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    try {
      await deleteAnnouncement(row.id);
      ElMessage.success('删除成功');
      getList();
    } catch (error) {
      console.error('删除公告失败:', error);
    }
  });
};

// 提交表单
const handleSubmit = async () => {
  try {
    await createAnnouncement(form.value);
    ElMessage.success(dialogType.value === 'add' ? '新增成功' : '编辑成功');
    dialogVisible.value = false;
    getList();
  } catch (error) {
    console.error('提交失败:', error);
  }
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.form-gonggao {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: 100vh;
}

.main-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.header {
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.add-button {
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.custom-table {
  margin: 16px 0;
  border-radius: 8px;
  overflow: hidden;
}

.content-cell {
  height: 100px;
  overflow: auto;
  padding: 8px;
  background-color: #fafafa;
  border-radius: 4px;
  line-height: 1.6;
}

.status-tag {
  padding: 6px 16px;
  border-radius: 4px;
}

.pagination {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
  padding: 16px 0;
}

:deep(.el-dialog) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 20px 24px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}

:deep(.el-dialog__body) {
  padding: 24px;
}

:deep(.el-dialog__footer) {
  padding: 16px 24px;
  border-top: 1px solid #e4e7ed;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>
